//
//  WelcomeViewController.swift
//  weibo
//
//  Created by 王、小胜 on 16/7/27.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WelcomeViewController: UIViewController {
    
    //加载界面
    
     override func loadView() {
        //直接用背景图片作为根视图
        
        view = backImageView
        setupUI()
    
    }
    //视图加载完成之后的后续处理，通常用来设置数ju

    override func viewDidLoad() {
        super.viewDidLoad()

        //异步加载用户头像
        iconView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatarUrl, placeholderImage: UIImage(named: "avatar_default_big"))
    }
    override func viewDidAppear(animated: Bool) {
        //1.更改约束。改变位置
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-UIScreen.mainScreen().bounds.height + 200)
        }

        //2.添加动画
        
        welcomeLable.alpha = 0
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.8,  animations: { () -> Void in
                    self.welcomeLable.alpha = 1
                    }) { (_) -> Void in
                        
                        print("好了")
            }

        }
 }

    //MARK: -懒加载控件
    //背景图
    private lazy var backImageView: UIImageView = UIImageView(imageName: "ad_background")
    //头像
    private lazy var iconView: UIImageView = {
        let iv = UIImageView(imageName: "avatar_default_big")
        //设置圆角
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        
        return iv
        
    }()
    
    //欢迎文字
    private lazy var welcomeLable: UILabel = UILabel(title: "欢迎回来，小崽子", fontSize: 18)
    

}

//MARK: -设置界面
extension WelcomeViewController {
    private func setupUI() {
        //1.添加控件
        view.addSubview(iconView)
        view.addSubview(welcomeLable)
        
        //2.自动布局
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        //欢迎文字
        welcomeLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
}
